package com.lvyq.myplayer.util

import com.lvyq.myplayer.model.lrc.LrcBean

/**
 * 文件描述：LrcUtil  歌词解析
 * 作者：lvyq
 * 创建时间：2025-03-13
 * 修改时间：2025-03-13
 */
object LrcUtil {

    /**
     * 歌词解析成集合
     */
    fun lrcParse(lrcContent:String):ArrayList<LrcBean>{
        var lrcList  = ArrayList<LrcBean>()
        //解析歌词
        if (lrcContent!=null && !lrcContent.equals("")){
            //获取每一行歌词
            val lines = lrcContent.split("\n")
            for (line in lines ){
                var lineLst = parseLineContent(line)
                lrcList.addAll(lineLst)
            }
            //排序
            lrcList.sortBy { it.startTime }

        }else{
            lrcList.add(LrcBean(0,"没有找到歌词"))
        }

      //  lrcList.forEach { println(it) }
        return lrcList
    }

    /**
     * 解析每一行 [02:53.66]哦 胡萝卜须抓住鼹鼠
     */
    private fun parseLineContent(line: String): ArrayList<LrcBean> {
        var list = ArrayList<LrcBean>()
        //分割line
        var split = line.split("]")
        //歌词
        var songContent = split.get(split.size-1)
        //时间  [02:53.66
        for ( index in 0..split.size-2){
            var time = split.get(index).substring(1)
            var timeSplit = time.split(":")

            var hour=0
            var min =0
            var sec =0f
            //含小时
            if (timeSplit.size==3){
                hour=timeSplit.get(0).replace("[","").toInt()*60 *60*1000
                min=timeSplit.get(1).toInt() *60*1000
                sec=timeSplit.get(2).toFloat()*1000
            }else{
                //分秒
                min=timeSplit.get(0).replace("[","").toInt() *60*1000
                sec=timeSplit.get(1).toFloat()*1000
            }
            list.add(LrcBean((hour+min+sec).toInt(),songContent))
        }
        return list
    }

    @JvmStatic
    fun main(args: Array<String>) {
        val lrcContent=
                "[00:21.60]先有鸡还是先有蛋\n" +
                "[00:25.70]先有女还是先有男\n"
        lrcParse(lrcContent)
    }
}